package cn.zyb.basic.service.Impl;

import cn.zyb.basic.mapper.BaseMapper;
import cn.zyb.basic.query.BaseQuery;
import cn.zyb.basic.service.IBaseService;
import cn.zyb.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zyb
 * @version 1.0.0
 * @date 2022年07月06日 11:12:47
 * @packageName cn.zyb.basic.service.Impl
 * @className BaseServiceImpl
 * @describe TODO
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
  @Autowired
  private BaseMapper<T> baseMapper;

  @Override
  public List<T> findAll() {
    return baseMapper.loadAll();
  }

  @Override
  public T findOne(Long id) {
    return baseMapper.loadById(id);
  }

  @Transactional
  @Override
  public void add(T t) {
    baseMapper.save(t);
  }

  @Transactional
  @Override
  public void del(Long id) {
    baseMapper.remove(id);
  }

  @Transactional
  @Override
  public void update(T t) {
    baseMapper.update(t);
  }

  @Override
  public PageList<T> queryPage(BaseQuery baseQuery) {
    //先查询总数量
    Integer count = baseMapper.queryCount(baseQuery);
    if (count == null || count == 0) {//没有数据
      return new PageList<>();
    }
    //再查询当前页的分页数据
    List<T> data = baseMapper.queryData(baseQuery);
    return new PageList<>(count, data);
  }

  @Transactional
  @Override
  public void patchDel(List<Long> ids) {
    baseMapper.patchDel(ids);
  }
}
